package com.kalvin.kvf.modules.dy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kalvin.kvf.modules.dy.mapper.LoginUserMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.kalvin.kvf.common.controller.BaseController;
import com.kalvin.kvf.common.dto.R;
import com.kalvin.kvf.modules.dy.entity.LoginUser;
import com.kalvin.kvf.modules.dy.service.LoginUserService;
import java.util.*;


/**
 * <p>
 * 服务端登录抖音账号 前端控制器
 * </p>
 * @since 2023-10-23 10:45:07
 */
@RestController
@RequestMapping("dy/loginUser")
public class LoginUserController extends BaseController {


    @Autowired
    private LoginUserService loginUserService;

    @Autowired
    private LoginUserMapper loginUserMapper;

    /**
     * 获取登录信息的cookie
     * @return
     * @throws Exception
     */
    @RequiresPermissions("dy:loginUser:add")
    @Transactional
    @PostMapping("/dyGetLoginCookies")
    public R dyGetLoginCookies() throws Exception{
        int code = loginUserService.dyGetLoginCookies();
        if(code == -1){
            return R.fail(500,"失败!");
        }
        return R.ok("成功!");
    }




    @PostMapping("/moniLogin")
    public R moniLogin() throws Exception{
        System.out.println("获取到webDriver对象");
        Map<String, Object> map =new HashMap<>();
        //抖音用户表登录 id
        Long loginUserId = (Long) map.get("loginUserId");
        //dirver
        WebDriver driver = (WebDriver) map.get("driver");

        driver.get("https://www.douyin.com/video/7234111131202112771");


        Thread.sleep(10000);

        //纯视频
        //WebElement ce7XkkTw = driver.findElement(By.xpath("//*[@id=\"douyin-right-container\"]/div[2]/div/div[1]/div[3]/div/div[2]/div[1]/div[2]/span"));

        //图文视频
        WebElement ce7XkkTw = driver.findElement(By.xpath("//*[@id=\"douyin-right-container\"]/div[2]/main/div[1]/div[2]/div/div[1]/div[2]/span"));
        System.out.println("============="+ce7XkkTw.getText());



        System.out.println("关闭浏览器");
        Thread.sleep(30000);
        driver.quit();

        //修改该抖音用户的登录状态  为   登陆中
        LoginUser loginUser = loginUserMapper.selectById(loginUserId);
        //用户登录状态   0：未登录  1：登陆中
        Integer isLogin = loginUser.getIsLogin();
        loginUser.setIsLogin(isLogin == 0 ? 1:0);
        loginUserMapper.updateById(loginUser);

        return R.ok("成功!");
    }



















    @RequiresPermissions("dy:loginUser:index")
    @GetMapping("index")
    public ModelAndView index() {
        return new ModelAndView("dy/loginUser");
    }

    @GetMapping(value = "edit")
    public ModelAndView edit(Long id) {
        ModelAndView mv = new ModelAndView("dy/loginUser_edit");
        LoginUser loginUser;
        if (id == null) {
            loginUser = new LoginUser();
        } else {
            loginUser = loginUserService.getById(id);
        }
        mv.addObject("editInfo", loginUser);
        return mv;
    }

    @GetMapping(value = "list/data")
    public R listData(LoginUser loginUser) {
        Page<LoginUser> page = loginUserService.listLoginUserPage(loginUser);
        return R.ok(page);
    }

    @RequiresPermissions("dy:loginUser:add")
    @PostMapping(value = "add")
    public R add(LoginUser loginUser) {
        loginUserService.save(loginUser);
        return R.ok();
    }

    @RequiresPermissions("dy:loginUser:del")
    @PostMapping(value = "batchdel")
    public R batchdel(@RequestParam("ids") List<Long> ids) {
        loginUserService.removeByIds(ids);
        return R.ok();
    }

    @RequiresPermissions("dy:loginUser:edit")
    @PostMapping(value = "edit")
    public R edit(LoginUser loginUser) {
        loginUserService.updateById(loginUser);
        return R.ok();
    }

    @RequiresPermissions("dy:loginUser:del")
    @PostMapping(value = "del/{id}")
    public R delUserAndCookie(@PathVariable Long id) {
        int code = loginUserService.delUserAndCookie(id);
        if(code == -1){
            R.fail(500,"删除失败!");
        }
        return R.ok();
    }

    @GetMapping(value = "get/{id}")
    public R get(@PathVariable Long id) {
        return R.ok(loginUserService.getById(id));
    }

}

